<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Admin_Controller extends MY_Controller {
	
	var $navigation;
	/**
	 * 
	 * Admin form object
	 * @var AdminForm
	 */
	public $form;
	/**
	 * 
	 * Admin Grid object
	 * @var AdminGrid
	 */
	public $grid;
	
	function __construct () {
		parent::__construct();
		
		define("_CONTROLPANEL","TRUE");

		$this->load->helper('ssl');
		
		$this->load->model('admin/admin_model');
		if (!$this->admin_model->logged_in()){
			$this->_redirect(admin_url('admin/index/login'));
			die();
		}
		
		$this->load->library('admin_navigation');
		$this->admin_navigation->parent_link('dashboard', 'Bảng điều khiển', admin_url('*/dashboard/index'));
		$this->admin_navigation->parent_link('users', $this->__('Users'));
		$this->admin_navigation->parent_link('news', $this->__('News'));
		
		$this->admin_navigation->parent_link('categories', $this->__('Categories'));
		
		
		$this->load->helper('string');
		$this->load->helper('directory');
		$this->load->helper('form');

		$this->load->library('headassets');
		
	
		$this->load->driver('cache');
		
		$modules = $this->module_model->get_module_folders();
		//var_dump($modules);

		$this->module_definitions = new stdClass();
		
		foreach ($modules as $module) {
			MY_Loader::define_module($module . '/');
		}
		
		$this->_setForm();
		$this->_setGrid();
	}
	
	private function _setForm(){
		if (is_null($this->form)){
			$this->load->library('AdminForm');
			$this->form = new AdminForm();
		}
		return $this->form;
	}
	
	private function _setGrid(){
		if (is_null($this->grid)){
			$this->load->library('AdminGrid');
			$this->grid = new AdminGrid();
		}
		return $this->grid;
	}
	
	function save_and_contine(){
		
	}
	
	function upload_image(&$data)
    {
        $dir = $this->config->item('path_users_uploads');
        $config['upload_path'] = './' . $dir;
        $config['allowed_types'] = 'jpg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']  = false;

        $this->load->library('upload');
        $this->upload->initialize($config);
        
        foreach($_FILES as $field => $file)
        {
            if (isset($data[$field . '_delete'])) {
                if (isset($data[$field . '_value'])) {
                    $path = $data[$field . '_value'];

                    if (file_exists($path)) {
                        unlink($path);
                    }
                }

                $data[$field] = '';
            }

            // No problems with the file
            if($file['error'] == 0)
            {
                // So lets upload
                if ($this->upload->do_upload($field))
                {
                    $out = $this->upload->data();
                    $data[$field] = $dir . $out['file_name'];
                }
                else
                {
                    $errors = $this->upload->display_errors();
                }
            }
            else if (isset($data[$field . '_value'])) {
                $data[$field] = $data[$field . '_value'];
            }
        }
    }
}